/*
 * Project: Bearing
 * Copyright (C) 2012 rdrrlabs gmail com.
 */

package com.alfray.bearing.core.app;

import android.content.Context;

import com.alfray.bearing.core.prefs.PrefsStorage;
import com.alfray.bearing.core.prefs.PrefsValues;

public class BearingAppImpl {

    public static final String DEFAULT_HOME_CLASS_PREFIX = "com.android.launcher";
    @SuppressWarnings("unused")
    private static final boolean DEBUG = true;
    public static final String TAG = BearingAppImpl.class.getSimpleName();

    private final Context mAppContext;

    /** Is first start of this app object. Used to skip/display intro. */
    private boolean mFirstStart = true;

    /** The current global prefs values. Allocated lazily. Use {@link #getPrefsValues()}. */
    private PrefsValues mPrefsValues;
    private PrefsStorage mPrefsStorage;

    //--private StatsWrapper mStats;

    public BearingAppImpl(Context applicationContext) {
        mAppContext = applicationContext;
    }

    public Context getAppContext() {
        return mAppContext;
    }

    public PrefsStorage getPrefsStorage() {
        return mPrefsStorage;
    }

    /**
     * Returns the global pref values, initializing it as needed the first time.
     */
    public PrefsValues getPrefsValues() {
        if (mPrefsValues == null) {
            synchronized(this) {
                if (mPrefsValues == null) {
                    mPrefsValues = new PrefsValues(mAppContext);
                }
            }
        }
        return mPrefsValues;
    }

    /** Returns the StatsWrapper, creating it as necessary. */
//    public StatsWrapper getStats() {
//        if (mStats == null) {
//            mStats = new StatsWrapper().start(getAppContext());
//        }
//        return mStats;
//    }

    /** Returns the StatsWrapper or null if not created yet. */
//    public StatsWrapper getStatsOrNull() {
//        return mStats;
//    }

    public void onCreate() {
    }

    public void onTerminate() {
        onLowMemory();
    }

    public void onLowMemory() {
        mPrefsStorage.beginWriteAsync(mAppContext);
        mPrefsStorage.endWriteAsync();
    }

    // ---

    public boolean isFirstStart() {
        return mFirstStart;
    }

    public void setFirstStart(boolean firstStart) {
        mFirstStart = firstStart;
    }
}
